const {get_wenchuan_info} = require('../../Class/get_wenchuan_info.js');
const getWenchuan = new get_wenchuan_info();
const {get_others} = require('../../Class/GetOthers.js');
const getOthers = new get_others();
let videoCtx = null;
Page({
  data: {
    cardCur: 0,
    indicator: false,
    margin: '30rpx',
    flag:false,
    show:false,
    autoplay:true
  },

  async onLoad(options) {
    wx.showLoading({
      title: '数据通信中',
      mask: true
    })
    const result = await getWenchuan.GetWenchuanInfo()
    const navbarHeight = getOthers.Set_navbar()
    const { activities,match,student,swiper_img } = result[0]
    const student_work = this.studentWork(student)
    this.setData({
      swiper_img,
      activities,
      match,
      student,
      topImg : student_work.topImg,
      bottomImg : student_work.bottomImg,
      bigItem : student_work.bigItem,
      navbarHeight,
      flag:true
    })
    wx.hideLoading()
  },

  // 返回上一层
  GoBack() {
    wx.navigateBack({
      delta: 1
    })
  },

  // 处理学生作品展示
  studentWork(work) {
    const filteredWork = work.filter(item => item.id !== 0);
    const topImg = filteredWork.filter((_, index) => index % 2 === 0);
    const bottomImg = filteredWork.filter((_, index) => index % 2 !== 0);
    const bigItem = {img: work[0].img, appid: work[0].appid,id:work[0].id};
    return {topImg, bottomImg, bigItem};
  },

  wenChuan(e) {
    const id = parseInt(e.currentTarget.dataset.id)
    switch (id) {
      case 0:
        wx.navigateTo({
          url: '/pages/wc-about/wc-about',
        })
        break;
      case 1:
        wx.navigateTo({
          url: '/pages/teachers-team/teachers-team',
        })
        break;
    }
  },
  // 跳转其他小程序
  GoWork(e){
    const id = e.currentTarget.dataset.id
    const {student} = this.data
    const { appid } = student[id]
    wx.navigateToMiniProgram({
      appId:appid,
      success:res=>{
        console.log(res)
      },
      fail:err=>{
        console.log(err)
      }
    })
  },
  // 文传活动
  activities(e){
    const id = e.currentTarget.dataset.id
    const { activities } = this.data
    const { url } = activities[id]
    wx.navigateTo({
      url: '/pages/official-account/openurl/openurl?url='+url,
    })
  },
  // 赛事活动
  GoMatch(e){
    const id = e.currentTarget.dataset.id
    const popup = this.data.match[id]
    this.setData({ 
      show: true,
      popup
     });

  },
  copyUrl(e){
    const {url} = this.data.popup
    wx.setClipboardData({
      data: url,
      success:res=>{
        wx.showToast({
          title: '复制成功',
        })
      }
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  // 监听页面
  onPageScroll(e) {
    var user = e.scrollTop
    if (user <= 100) {
      this.setData({
        num: 0,
      })
    } else if (100 < user < 300) {
      this.setData({
        num: user / 300,
      })
    } else if (user > 300) {
      this.setData({
        num: 0.5,
      })
    }
  },

  // 轮播处理
  bindPlay(){
    this.setData({autoplay:false})
  },
  bindPause(){
    this.setData({autoplay:true})
  },
  cardSwiper(e){
    videoCtx = wx.createVideoContext('myVideo', this)
    videoCtx.pause()
    this.setData({
      cardCur: e.detail.current,
      autoplay:true
    })
  }
})